package controller;

import java.rmi.RemoteException;

import server.FirewallException;
import server.GlobalLog;
import server.Log;
import server.NotExpertException;
import server.Protocol;
import server.ReadWriteException;
import server.Rule;
import server.RulesTableType;
import server.Log.directOpt;



/**
 * <p>Title: FireWall Project. FirewallSession Module </p>
 * <p>Description: The FirewallSession module is a conector between the client and server.
 * 					it provide to the user to transfer orders ,
 * 					create or remove rules in the firewall, show all firewall rules, change log state,
 * 					clean & read log</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * @author FW team
 * @version 1.0
 */
public class FirewallSession {

	public static Rule[] test_rules;	
	public static Log[]  test_logs;	
	public static Protocol[]  test_protocols;
	public static GlobalLog[] test_global_log;
	public static boolean is_debug_init_done = false;
	
	
	public static server.FirewallSession firewall;
	public static boolean is_server_impl = true;
	public static boolean is_debug_print = false;
	
	/**
	 * debug print for FirewallSession 
	 **/
	public  void debug_print(String s){
		if ( is_debug_print){
			System.out.println("DEBUG:" + s);
		}
	}
	
	/**
	 * initilizate static firewall instance
	 * Have to be called before any use of FirewallSession class
	 **/
	public void set_firewall(server.FirewallSession _firewall){
		firewall = _firewall;
	}
	
	/**
	 * remove static firewall instance.
	 * Have to be called this function before exit!
	 **/
	public void unset_firewall(server.FirewallSession _firewall){
		firewall = null;
	}
	
	
	public FirewallSession(){
		if ( ! is_server_impl  &&  !is_debug_init_done){
			debug_init();
			is_debug_init_done = true;
		}
	}

	private void debug_init() {
		test_rules = new Rule[11];
		test_rules[0]  = new Rule(1, 80, "http", Rule.permissionOpt.DENY );
		test_rules[1]  = new Rule(2, "123.123.123.123",60, Rule.permissionOpt.DENY );
		test_rules[2]  = new Rule(2, "123.123.123.123",60, Rule.permissionOpt.DENY );
		test_rules[3]  = new Rule(3, "234.234.234.234", 70, Rule.permissionOpt.DENY );
		test_rules[4]  = new Rule(4, "555.666.777.888",90, Rule.permissionOpt.DENY );
		test_rules[5]  = new Rule(5, "555.666.777.888",660, Rule.permissionOpt.DENY );
		test_rules[6]  = new Rule(6, null, 70, Rule.permissionOpt.DENY );
		test_rules[7]  = new Rule(7, "555.666.777.888",60, Rule.permissionOpt.ALLOW );
		test_rules[8]  = new Rule(8, null,40, Rule.permissionOpt.DENY );
		test_rules[9]  = new Rule(9, 60, "hahaha", Rule.permissionOpt.DENY );
		test_rules[10] = new Rule(10, "555.666.777.888",60, Rule.permissionOpt.DENY );
		
		test_logs = new Log[8];
		test_logs[0] = new Log((long) 4444,  Log.directOpt.IN, "10:10:20:1", "10:10:20:2", 80, 2078, 80, "http", Log.actionOpt.ACCEPT) ;
		test_logs[1] = new Log((long) 4444,  Log.directOpt.IN, "10:10:20:1", "10:10:20:2", 80, 2078, 80, "http", Log.actionOpt.ACCEPT) ;
		test_logs[2] = new Log((long) 4444,  Log.directOpt.IN, "10:10:20:1", "10:10:20:2", 80, 2078, 80, "http", Log.actionOpt.ACCEPT) ;
		test_logs[3] = new Log((long) 4444,  Log.directOpt.IN, "10:10:20:1", "10:10:20:2", 80, 2078, 80, "http", Log.actionOpt.ACCEPT) ;
		test_logs[4] = new Log((long) 4444,  Log.directOpt.IN, "10:10:20:1", "10:10:20:2", 80, 2055, 80, "http", Log.actionOpt.ACCEPT) ;
		test_logs[5] = new Log((long) 4444,  Log.directOpt.IN, "10:10:20:1", "10:10:20:2", 80, 2078, 80,null, Log.actionOpt.ACCEPT) ;
		test_logs[6] = new Log((long) 4444,  Log.directOpt.IN, "10:10:20:1", "10:10:20:2", 80, 2078, 80, "http", Log.actionOpt.ACCEPT) ;
		test_logs[7] = new Log((long)4444,  Log.directOpt.IN, "10:10:20:1", "10:10:20:2", 80, 2078, 80, "http", Log.actionOpt.ACCEPT) ;
		
		test_global_log = new GlobalLog[2];
		test_global_log[0] = new GlobalLog( 2000, "Rule added 1") ;
		test_global_log[1] = new GlobalLog( 2001, "Rule added 2") ;
		
		test_protocols = new Protocol[2];
		test_protocols[0] = new Protocol( "Protocol1", "ProtocolIN", "ProtocolOUT") ;
		test_protocols[1] = new Protocol( "Protocol2", "ProtocolIN11", "ProtocolOUT11") ;
	}

	
	public boolean isOn() throws RemoteException, FirewallException
	{
		
		if ( is_server_impl ){
			return firewall.isOn();
		}else{
			return false;
		}
	}
	
	public  void turnOff()throws RemoteException, FirewallException, ReadWriteException {
		debug_print("turn ON!");
		
		if ( is_server_impl ){
			firewall.turnOff();
		}else{
			
		}
	}

	public  void turnOn()throws RemoteException, FirewallException, ReadWriteException {
		debug_print("turn OFF!");
		
		if ( is_server_impl ){
			firewall.turnOn();
		}else{
		}
	}
	
	public boolean isReadWrite()throws RemoteException {
		if ( is_server_impl ){
			return firewall.isReadWrite();
		}else{
			return true;
		}
	}
	public boolean isExpert()  throws RemoteException {
		debug_print("isExpert");
		if ( is_server_impl ){
			return firewall.isExpert();
		}else{
			return true;
		}
	}
	
	



	public  Rule[] getRules(RulesTableType type, String ruleset)throws RemoteException, FirewallException
	{
		debug_print("getRules! for " + ruleset);
		
		if ( is_server_impl ){
			return  firewall.getRules( type,  ruleset);
		}else{
			return  test_rules;
		}
	}
	
	
	public  void addRule(RulesTableType type, String ruleset, Rule rule) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		debug_print("addRule to " +ruleset + " rule " + rule + "port" +rule.port);
		if ( is_server_impl ){
			 firewall.addRule( type,  ruleset,  rule);
		}else{
			test_rules[0] = rule;
		}
	}

	public  void deleteRule(RulesTableType type, String ruleset, int id)throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		debug_print("deleteRule : " + id + " from " + ruleset);
		if ( is_server_impl ){
			 firewall.deleteRule( type,  ruleset,  id);
		}else{
			
		}
		
	}
	
	public  void setRuleSet(String ruleset)throws RemoteException, FirewallException, ReadWriteException
	{
		debug_print("setRuleSet!"+ ruleset);
		
		if ( is_server_impl ){
			 firewall.setRuleSet( ruleset);
		}else{
		}
	}
	
	public void createRuleSet(String ruleset) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		debug_print("createRuleSet!"+ ruleset);
		if ( is_server_impl ){
			firewall.createRuleSet( ruleset);
		}else{
			
		}
		
	}
		
	
	public String getCurrentRuleSet() throws RemoteException {
		
		debug_print("getCurrentRuleSet!");
		if ( is_server_impl ){
			return firewall.getCurrentRuleSet();
		}else{
			return "ruleset1111";
		}
	}
	
	public String[] getRuleSets() throws RemoteException, FirewallException{
		debug_print("getRuleSets!");
		if ( is_server_impl ){
			return firewall.getRuleSets();
		}else{
			String[] s = {"ruleset1111", "ruleset2"};
			return s;
		}
	}
	
	public void deleteRuleSet(String ruleset) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		debug_print("deleteRuleSet! " + ruleset);
		if ( is_server_impl ){
			firewall.deleteRuleSet(ruleset); 
		}else{
		}
	}

	public void enableLog() throws RemoteException, FirewallException, ReadWriteException{
		debug_print("enableLog");
		
		if ( is_server_impl ){
			firewall.enableLog() ;
		}		
	}
	
	public void disableLog() throws RemoteException, FirewallException, ReadWriteException{
		debug_print("disableLog");
		
		if ( is_server_impl ){
			firewall.disableLog() ;
		}			
	}

	public Log[] getLogs(Integer days, Log.typeOpt type,String protocol, String ip, Integer port, directOpt dir ) throws RemoteException, FirewallException{
		debug_print("getLogs  protocol="+protocol + " ip= " + ip);
		if ( is_server_impl ){
			return firewall.getLogs( days,type, protocol,  ip,  port,  dir);
		}else{
			return test_logs;
		}
	}



	public GlobalLog[] getGlobalLogs(int days) throws RemoteException, FirewallException
	{
		debug_print("getLoggerSessionsReports " + days);
		if ( is_server_impl ){
			return firewall.getGlobalLogs( days);
		}else{
			return test_global_log;
		}
	}
	public  void addProtocol(Protocol protocol) throws RemoteException, FirewallException, ReadWriteException, NotExpertException{
		debug_print("add protocol : " + protocol.protocol +" IN:"+protocol.inReg+",OUT:"+ protocol.outReg);
		if ( is_server_impl ){
			firewall.addProtocol(protocol);
		}
	}
	
	
	public void deleteProtocol(String name) throws RemoteException, FirewallException, ReadWriteException, NotExpertException{
		debug_print("deleteProtocol protocol : " + name );
		if ( is_server_impl ){
			firewall.deleteProtocol(name);
		}
	}
	
	public void updateProtocol(Protocol protocol) throws RemoteException, FirewallException, ReadWriteException, NotExpertException{
		debug_print("updateProtocol protocol : " + protocol.protocol +" IN:"+protocol.inReg+",OUT:"+ protocol.outReg);
		if ( is_server_impl ){
			firewall.updateProtocol(protocol);
		}
	}
	
	public Protocol[] getProtocols() throws RemoteException, FirewallException{
		debug_print("getProtocols");
		if ( is_server_impl ){
			return firewall.getProtocols();
		}else{
			return test_protocols;
		}
	}

	public boolean isLogEnabled() throws RemoteException{
		debug_print("isLogEnabled");
		if ( is_server_impl ){
			return firewall.isLogEnabled();
		}else{
			return true;
		}
	}

}
